//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet;
using OpenLS.Spreadsheet.Data;

namespace OpenLS.Spreadsheet.Pivot
{
    internal class PivotAreaReference : IOfficeXmlSerializable
    {
        private const string xmlName = "reference";
        private readonly List<Index> _x = new List<Index>();
        private Boolean _avgSubtotal;
        private Boolean _byPosition;
        private UInt32? _count;
        private Boolean _countASubtotal;
        private Boolean _countSubtotal;
        private Boolean _defaultSubtotal;
        private ExtensionList _extLst;
        private UInt32? _field;
        private Boolean _maxSubtotal;
        private Boolean _minSubtotal;
        private Boolean _productSubtotal;
        private Boolean _relative;
        private Boolean _selected = true;
        private Boolean _stdDevPSubtotal;
        private Boolean _stdDevSubtotal;
        private Boolean _sumSubtotal;
        private Boolean _varPSubtotal;
        private Boolean _varSubtotal;

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                _field = c.GetOptionalUInteger("field");
                _count = c.GetOptionalUInteger("count");
                _selected = c.GetOptionalBoolean("selected", true);
                _byPosition = c.GetOptionalBoolean("byPosition", false);
                _relative = c.GetOptionalBoolean("relative", false);
                _defaultSubtotal = c.GetOptionalBoolean("defaultSubtotal", false);
                _sumSubtotal = c.GetOptionalBoolean("sumSubtotal", false);
                _countASubtotal = c.GetOptionalBoolean("countASubtotal", false);
                _avgSubtotal = c.GetOptionalBoolean("avgSubtotal", false);
                _maxSubtotal = c.GetOptionalBoolean("maxSubtotal", false);
                _minSubtotal = c.GetOptionalBoolean("minSubtotal", false);
                _productSubtotal = c.GetOptionalBoolean("productSubtotal", false);
                _countSubtotal = c.GetOptionalBoolean("countSubtotal", false);
                _stdDevSubtotal = c.GetOptionalBoolean("stdDevSubtotal", false);
                _stdDevPSubtotal = c.GetOptionalBoolean("stdDevPSubtotal", false);
                _varSubtotal = c.GetOptionalBoolean("varSubtotal", false);
                _varPSubtotal = c.GetOptionalBoolean("varPSubtotal", false);
                _x.Clear();
                _x.AddRange(c.ReadOptionalWhile<Index>("x"));
                _extLst = c.ReadOptionalElement<ExtensionList>("extLst");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                c.SetOptionalUInteger("field", _field);
                c.SetOptionalUInteger("count", _count);
                c.SetOptionalBoolean("selected", _selected, true);
                c.SetOptionalBoolean("byPosition", _byPosition, false);
                c.SetOptionalBoolean("relative", _relative, false);
                c.SetOptionalBoolean("defaultSubtotal", _defaultSubtotal, false);
                c.SetOptionalBoolean("sumSubtotal", _sumSubtotal, false);
                c.SetOptionalBoolean("countASubtotal", _countASubtotal, false);
                c.SetOptionalBoolean("avgSubtotal", _avgSubtotal, false);
                c.SetOptionalBoolean("maxSubtotal", _maxSubtotal, false);
                c.SetOptionalBoolean("minSubtotal", _minSubtotal, false);
                c.SetOptionalBoolean("productSubtotal", _productSubtotal, false);
                c.SetOptionalBoolean("countSubtotal", _countSubtotal, false);
                c.SetOptionalBoolean("stdDevSubtotal", _stdDevSubtotal, false);
                c.SetOptionalBoolean("stdDevPSubtotal", _stdDevPSubtotal, false);
                c.SetOptionalBoolean("varSubtotal", _varSubtotal, false);
                c.SetOptionalBoolean("varPSubtotal", _varPSubtotal, false);
                c.WriteElements(_x);
                c.WriteOptionalElement(_extLst);
            }
        }

        #endregion
    }
}